package selcoursesys.service.impl;

import com.opensymphony.xwork2.ActionContext;
import selcoursesys.dao.CourseDAO;
import selcoursesys.dao.SelectionDAO;
import selcoursesys.dao.StudentDAO;
import selcoursesys.dao.impl.CourseDAOImpo;
import selcoursesys.dao.impl.SelectionDAOImpl;
import selcoursesys.dao.impl.StudentDAOImpl;
import selcoursesys.entity.*;
import selcoursesys.service.AdminService;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Lpbdz on 2017/4/14.
 */
public class AdminServiceImpl implements AdminService{
    //统计选课情况
    public boolean showSelectSitu(){
        CourseDAO courseDAO=new CourseDAOImpo();
        List<CourseEntity> list=courseDAO.queryAllCourse();
        List<CourseInfo> courseInfos=new LinkedList<CourseInfo>();
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            CourseEntity courseEntity=(CourseEntity)iterator.next();
            System.out.println(courseEntity.getCourseid()+" "+courseEntity.getCoursename()+" "+courseEntity.getTeacherEntity().getTname()+" "+courseEntity.getCoursetype()+" "+courseEntity.getLnum()+" "+courseEntity.getCnum());
            courseInfos.add(new CourseInfo(courseEntity.getCourseid(),courseEntity.getCoursename(),courseEntity.getTeacherEntity().getTname(),
                    courseEntity.getCoursetype(),courseEntity.getLnum(),courseEntity.getCnum()));
        }
        if(list==null){
            return false;
        }
        ActionContext.getContext().put("selectsitu",courseInfos);
        return true;
    }
    //查看选课详情
    public boolean showDeatil(String courseid){
        //通过 cid Get 选课学生);
        List<StudentInfo> studentInfos=new LinkedList<StudentInfo>();
        SelectionDAO selectionDAO=new SelectionDAOImpl();
        List<SelectionEntity> list=selectionDAO.querySelectCid(courseid);
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            SelectionEntity selectionEntity=(SelectionEntity)iterator.next();

            StudentDAO studentDAO=new StudentDAOImpl();
            List<StudentEntity> studentEntities=studentDAO.queryStudentId(selectionEntity.getStuid());
            Iterator stuiter=studentEntities.iterator();
            while(stuiter.hasNext()){
                StudentEntity studentEntity=(StudentEntity)stuiter.next();
                System.out.println(studentEntity.getStuid()+" "+studentEntity.getStuname()+" "+studentEntity.getClazzByClassid().getClassname()+" "+studentEntity.getAcademyByAid().getAname());
                studentInfos.add(new StudentInfo(studentEntity.getStuid(),studentEntity.getStuname() ,studentEntity.getAcademyByAid().getAname(),studentEntity.getClazzByClassid().getClassname()));
            }
        }
        ActionContext.getContext().put("stuinfo",studentInfos);
        return true;
    }

    //根据课程名称查找
    public boolean searchCourseName(String keyword){
        CourseDAO courseDAO=new CourseDAOImpo();
        List<CourseInfo> courseInfos=new LinkedList<CourseInfo>();
        List<CourseEntity> list=courseDAO.queryCourseCourseName(keyword);
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            CourseEntity courseEntity=(CourseEntity)iterator.next();
            courseInfos.add(new CourseInfo(courseEntity.getCourseid(),courseEntity.getCoursename(),courseEntity.getTeacherEntity().getTname(),
                    courseEntity.getCoursetype(),courseEntity.getLnum(),courseEntity.getCnum()));
        }
        if(list==null){
            return false;
        }
        ActionContext.getContext().put("selectsitu",courseInfos);
        return true;
    }
    //管理员搜索选课学生
    public boolean searchSelectInfo(String keyword,String courseid){
        //通过课程id得到选课学生
        List<StudentInfo> studentInfos=new LinkedList<StudentInfo>();
        SelectionDAO selectionDAO=new SelectionDAOImpl();
        List<SelectionEntity> list=selectionDAO.querySelectCid(courseid);
        Iterator iterator=list.iterator();
        while(iterator.hasNext()){
            SelectionEntity selectionEntity=(SelectionEntity)iterator.next();
            StudentDAO studentDAO=new StudentDAOImpl();
            List<StudentEntity> studentEntities=studentDAO.queryStudentId(selectionEntity.getStuid());
            Iterator stuiter=studentEntities.iterator();
            while(stuiter.hasNext()){
                StudentEntity studentEntity=(StudentEntity)stuiter.next();
                String info="";
                info=info+studentEntity.getStuid()+","+studentEntity.getStuname()+","+studentEntity.getAcademyByAid().getAname()+","+studentEntity.getClazzByClassid().getClassname();
                if(info.indexOf(keyword)!=-1){
                    studentInfos.add(new StudentInfo(studentEntity.getStuid(),studentEntity.getStuname() ,studentEntity.getAcademyByAid().getAname(),studentEntity.getClazzByClassid().getClassname()));
                }
            }
        }
        if(studentInfos==null){
            return false;
        }
        ActionContext.getContext().put("stuinfo",studentInfos);
        return true;
    }
}
